package com.xiaomengkuan.product.controller;

import com.xiaomengkuan.common.util.R;
import com.xiaomengkuan.product.entity.tdo.TDO;
import com.xiaomengkuan.product.service.LikeAndFavouriteService;
import com.xiaomengkuan.product.util.RelationSelector;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author Faraj Mujey
 * -- Creation Time: 2021-01-27 2:01 下午, File Encoding: utf8 --
 */
@RestController
@RequestMapping("product")
@Api(tags = "喜欢商品")
public class LikeController {
    @Resource
    private LikeAndFavouriteService likeAndFavouriteService;

    /**
     * 喜欢某件商品
     *
     * @param userId    用户id
     * @param productId 商品id
     * @return R
     */
    @ApiOperation(value = "喜欢某件商品")
    @GetMapping("like/{userId}/{productId}")
    public R like(@PathVariable("userId") String userId,
                  @PathVariable("productId") String productId) {
        TDO flag = likeAndFavouriteService.like(userId, productId,
                RelationSelector.LIKE);
        return flag.isFlag() ? R.ok().detail(flag.getDetail()) :
                R.err().detail(flag.getDetail());
    }

    /**
     * 取消喜欢某件商品
     *
     * @param userId    用户id
     * @param productId 商品id
     * @return R
     */
    @ApiOperation(value = "取消喜欢某件商品")
    @GetMapping("unlike/{userId}/{productId}")
    public R unlike(@PathVariable("userId") String userId,
                  @PathVariable("productId") String productId) {
        TDO tdo = likeAndFavouriteService.unlike(userId, productId,
                RelationSelector.LIKE);
        return tdo.isFlag() ? R.ok().detail(tdo.getDetail()) :
                R.err().detail(tdo.getDetail());

    }

}
